package com.wm.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Area;
import com.wm.bean.City;
import com.wm.comm.Constant;
import com.wm.comm.PinYinUtil;
import com.wm.service.AreaService;
import com.wm.service.CityService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;
import com.wm.web.ui.OptionsInt;
import com.wm.web.ui.OptionsLong;

public class AdminArea extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminArea.class);
	
	private CityService cityService;
	private AreaService areaService;

	public CityService getCityService() {
		return cityService;
	}

	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}
	
	public AreaService getAreaService() {
		return areaService;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	private List cityList = new ArrayList();
	private List isShowList = new ArrayList();

	
	public List getCityList() {
		return cityList;
	}

	public void setCityList(List cityList) {
		this.cityList = cityList;
	}

	public List getIsShowList() {
		return isShowList;
	}

	public void setIsShowList(List isShowList) {
		this.isShowList = isShowList;
	}
	
	private void setCityListValues(){
		cityList.add(new OptionsLong(-1, "请选择"));
		List l = this.getCityService().findCitysByIsShow(1, "id", 0);
		for(int i=0;i<l.size();i++){
			City c = (City) l.get(i);
			cityList.add(new OptionsLong(c.getId(), c.getCityName()));
		}
	}
	
	private void setIsShowListValues(){
		isShowList = Constant.IS_SHOW;
	}
	
	private void initValues(){
		setIsShowListValues();
		setCityListValues();
	}
	
	private void listInitValues(){
		setIsShowListValues();
		setCityListValues();
	}


	private long id;					//主键ID
	private String areaName;			//地区名称
	private String areaInitials;		//首字母
	//private String areaAnglic;		//地区的简拼
	//private String areaPhonetic;		//地区的全拼
	private long cityId = -1;			//城市ID
	private int isShow = -1;			//是否显示
	private int orderNum;				//排序字段
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	
	public String getAreaName() {
		return areaName;
	}

	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	public String getAreaInitials() {
		return areaInitials;
	}

	public void setAreaInitials(String areaInitials) {
		this.areaInitials = areaInitials;
	}

	public long getCityId() {
		return cityId;
	}

	public void setCityId(long cityId) {
		this.cityId = cityId;
	}

	public int getIsShow() {
		return isShow;
	}

	public void setIsShow(int isShow) {
		this.isShow = isShow;
	}

	public int getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(int orderNum) {
		this.orderNum = orderNum;
	}

	private PageList pageList;
	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}	

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		listInitValues();
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/admin_area.action?cityId=" + this.cityId + "&isShow=" + this.isShow);
		this.setPageList(this.getAreaService().findAreasByCityIdIsShow(this.cityId,this.isShow,"id", 1, pages));
		return SUCCESS;
	}
	
	public String add(){
		initValues();
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		//添加
		Area area = new Area();
		area.setAreaName(this.areaName);
		area.setAreaInitials(this.areaInitials.toUpperCase());
		area.setAreaAnglic(PinYinUtil.getPinYinHeadChar(this.areaName));
		area.setAreaPhonetic(PinYinUtil.getPinYin(this.areaName));
		area.setCityId(this.cityId);
		area.setIsShow(this.isShow);
		area.setOrderNum(this.orderNum);
		area.setOrderTime(new Date());
		area.setAddTime(new Date());
		try {
			this.getAreaService().saveArea(area);
		} catch (Exception e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		initValues();
		this.setAction("doupdate");
		Area area = this.getAreaService().findAreaByid(this.id);
		this.setAreaName(area.getAreaName());
		this.setAreaInitials(area.getAreaInitials());
		//this.setAreaAnglic(area.getAreaAnglic());
		//this.setAreaPhonetic(area.getAreaPhonetic());
		this.setCityId(area.getCityId());
		this.setIsShow(area.getIsShow());
		this.setOrderNum(area.getOrderNum());
		return INPUT;
	}
	
	public String doupdate(){
		//添加
		Area area = this.getAreaService().findAreaByid(this.id);
		area.setAreaName(this.areaName);
		area.setAreaInitials(this.areaInitials.toUpperCase());
		area.setAreaAnglic(PinYinUtil.getPinYinHeadChar(this.areaName));
		area.setAreaPhonetic(PinYinUtil.getPinYin(this.areaName));
		area.setCityId(this.cityId);
		area.setIsShow(this.isShow);
		area.setOrderNum(this.orderNum);
		area.setOrderTime(new Date());
		area.setAddTime(new Date());
		try {
			this.getAreaService().updateArea(area);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String orders(){
		Area area = this.getAreaService().findAreaByid(this.id);
		area.setOrderTime(new Date());
		try {
			this.getAreaService().updateArea(area);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		this.getAreaService().removeAreaById(this.id);
		return "index";
	}
	
	
}
